<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/11/18
 * Time: 16:45
 */

namespace app\index\model;


use think\Model;
use traits\model\SoftDelete;

class Check extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = true;
    
    /*
     * 获得上一次的检查结果
     * @param int $uid 用户id
     * @param string $itempid 监测项目id
     * @param date $date 录入时间
     * @return Model 上一次的检查结果的结果模型对象
     *
     * */
    public function getLastCheck($uid,$itempid,$date){
        $w['uid'] = $uid;
        $w['date'] = ['<',$date];
         
        $w['itemid'] = ['like',$itempid.'%'];
        $res = $this->where($w)->order('date','desc')->find();
        $lastcheck = $res;

        return $lastcheck;
    }


    /*
     * 今天的餐前大剂量总量
     * @param int $uid 用户id
     * @return float 餐前大剂量总量
     *
     *
     * */
    public function getbeforeeatinsulinOfToday($uid){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','2%'];
        
        $beforeeatinsulin = $this->where($w)->whereTime('date','today' )->sum('value');
//        dump($this->getLastSql());
        return $beforeeatinsulin;

    }


    /*
     * 今天的临时大剂量总量
     * @param int $uid 用户id
     * @return float 临时大剂量总量
     *
     *
     * */
    public function gettemporaryinsulinOfToday($uid){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','3%'];

        $temporaryinsulin = $this->where($w)->whereTime('date','today' )->sum('value');
        return $temporaryinsulin;

    }

    /*
     * 临时大剂量注射情况
     * @param int $uid 用户id
     * @return float 临时大剂量总量
     *
     *
     * */
    public function gettemporaryinsulinListOfToday($uid){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','3%'];

        $temporaryinsulinlist = $this->field('cid,itemid,date,value')->where($w)->whereTime('date','today' )->select();
        if(!empty($temporaryinsulinlist)){
            foreach ($temporaryinsulinlist as $i => $v){
                $temporaryinsulinlist[$i]['time'] = date('H:i',strtotime($temporaryinsulinlist[$i]['date']));

                $itemidsarr = explode(',',$v['itemid'] );
                $itemid = $itemidsarr[1];
                $itemmodel = new Item();
                $item = $itemmodel->where('itemid',$itemid)->find();
                $temporaryinsulinlist[$i]['itemname'] = $item['itemname'];

            }
        }else{
            return null;
        }
        return $temporaryinsulinlist;

    }



    /*
     * 历史记录的餐前大剂量总量
     * @param int $uid 用户id
     * @param int $date 时间，例如2018/08/01
     * @return float 餐前大剂量总量
     *
     *
     * */
    public function getbeforeeatinsulinOfDate($uid,$date){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','2%'];

//        $time1 = strtotime($date);
        if(empty($date)){

            $time1 =  strtotime(date('Y-m-d',time())) ;


        }else{
            $time1 = strtotime($date);
            $time1 =  strtotime(date('Y-m-d',$time1)) ;

        }
        $time2 = strtotime('+1day',$time1);


        $beforeeatinsulin = $this->where($w)->whereTime('date','between', [$time1,$time2])->sum('value');
//        dump($this->getLastSql());
        return number_format($beforeeatinsulin,2);

    }


    /*
     * 历史记录的临时大剂量总量
     * @param int $uid 用户id
     * @param date $date 某一天时间
     * @return float 临时大剂量总量
     *
     *
     * */
    public function gettemporaryinsulinOfDate($uid,$date){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','3%'];

//        $time1 = strtotime($date);
        if(empty($date)){

            $time1 =  strtotime(date('Y-m-d',time())) ;


        }else{
            $time1 = strtotime($date);
            $time1 =  strtotime(date('Y-m-d',$time1)) ;

        }
        $time2 = strtotime('+1day',$time1);

        $temporaryinsulin = $this->where($w)->whereTime('date','between', [$time1,$time2])->sum('value');
        return number_format($temporaryinsulin,2) ;

    }



    /*
     * 历史记录临时大剂量注射情况
     * @param int $uid 用户id
     * @param date $date 某一天时间
     * @return float 临时大剂量总量
     *
     *
     * */
    public function gettemporaryinsulinListOfDate($uid,$date){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','3%'];


//        $time1 = strtotime($date);
        if(empty($date)){

            $time1 =  strtotime(date('Y-m-d',time())) ;


        }else{
            $time1 = strtotime($date);
            $time1 =  strtotime(date('Y-m-d',$time1)) ;

        }
        $time2 = strtotime('+1day',$time1);

        $temporaryinsulinlist = $this->field('cid,itemid,date,value,note')->where($w)->whereTime('date','between', [$time1,$time2])->select();
//        dump($this->getLastSql());

        //设置默认值
        $itemmodel = new Item();
        $chilrenitem = $itemmodel->where('pid',3)->select();//所有的血糖子项目
        $checkarr = array();
        foreach ($chilrenitem as $ci => $cv){//每一个都为空
            $checkarr[$cv['itemname']] = array(
                'cid' => null,
                'itemid' => $cv['itemid'],
                'date' => null,
                'value' => null,
                'note' => null,
                'itemname' => $cv['itemname'],
                'pid' => 3
            );
        }



        if(!empty($temporaryinsulinlist)){
			//halt($temporaryinsulinlist);
            foreach ($temporaryinsulinlist as $i => $v){
				//halt($v['itemid']);
                $temporaryinsulinlist[$i]['time'] = date('H:i',strtotime($temporaryinsulinlist[$i]['date']));

                $itemidsarr = explode(',',$v['itemid'] );
                $itemid = $itemidsarr[1];

                $item = $itemmodel->where('itemid',$itemid)->find();
                $temporaryinsulinlist[$i]['itemname'] = $item['itemname'];


                //把结果写入结果数据，对号入座
                $checkarr[$item['itemname']]['cid'] =  $v['cid'];
                $checkarr[$item['itemname']]['itemid'] =  $itemid;
                $checkarr[$item['itemname']]['pid'] =  $itemidsarr[0];
                $checkarr[$item['itemname']]['date'] =  $v['date'];
                $checkarr[$item['itemname']]['value'] =  $v['value'];
                $checkarr[$item['itemname']]['note'] =  $v['note'];



            }

            return $checkarr;
        }else{
            return $checkarr;
        }




    }



    /*
     * 历史记录血糖列表
     * @param int $uid 用户id
     * @param date $date 某一天时间
     * @return float 临时大剂量总量
     *
     *
     * */
    public function getBloodSugarCheckOfDate($uid,$date){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','1%'];


//        $time1 = strtotime($date);
        if(empty($date)){

            $time1 =  strtotime(date('Y-m-d',time())) ;


        }else{
            $time1 = strtotime($date);
            $time1 =  strtotime(date('Y-m-d',$time1)) ;

        }
        $time2 = strtotime('+1day',$time1);

        $bscheck = $this->where($w)->field('cid,itemid,date,value,health,type,note,reason,reasonid')->whereTime('date','between', [$time1,$time2])->select();

        
        //设置默认值
        $itemmodel = new Item();
        $chilrenitem = $itemmodel->where('pid',1)->select();//所有的血糖子项目
        $checkarr = array();
        foreach ($chilrenitem as $i => $v){//每一个都为空
            $checkarr[$v['itemname']] = array(
                'cid' => null,
                'itemid' => $v['itemid'],
                'date' => null,
                'value' => null,
                'health' => null,
                'type' => null,
                'note' => null,
                'reason' => null,
                'itemname' => $v['itemname'],
                'pid' => 1
            );
        }

        if (!empty($bscheck)){



            foreach ($bscheck as $i=>$v){
                $itemidsarr = explode(',',$v['itemid'] );
                $itemid = $itemidsarr[1];
                $itemmodel = new Item();
                $item = $itemmodel->where('itemid',$itemid)->find();
                $bscheck[$i]['itemname'] = $item['itemname'];

                //把结果写入结果数据，对号入座
                $checkarr[$item['itemname']]['cid'] =  $v['cid'];
                $checkarr[$item['itemname']]['itemid'] =  $itemid;
                $checkarr[$item['itemname']]['pid'] =  $itemidsarr[0];
                $checkarr[$item['itemname']]['date'] =  $v['date'];
                $checkarr[$item['itemname']]['value'] =  $v['value'];
                $checkarr[$item['itemname']]['health'] =  $v['health'];
                $checkarr[$item['itemname']]['type'] =  $v['type'];
                $checkarr[$item['itemname']]['note'] =  $v['note'];
                $checkarr[$item['itemname']]['reason'] =  $v['reason'];
                $checkarr[$item['itemname']]['reasonid'] =  $v['reasonid'];


            }
            return $checkarr;

        }else{
            return $checkarr;
        }

    }

    /*
     * 查看某一天记录餐前大剂量列表情况
     * @param int $uid 用户id
     * @param date $date 某一天时间
     * @return float 临时大剂量总量
     *
     *
     * */
    public function getBeforeeatingListOfDate($uid,$date){
        $w['uid'] = $uid;
        $w['itemid'] = ['like','2%'];

        
        if(empty($date)){

            $time1 =  strtotime(date('Y-m-d',time())) ;
            
        }else{
            $time1 = strtotime($date);
            $time1 =  strtotime(date('Y-m-d',$time1)) ;

        }


//        $time1 = strtotime($date);
        $time2 = strtotime('+1day',$time1);

        $Beforeeatinglist = $this->where($w)->field('cid,itemid,date,value,note')->whereTime('date','between', [$time1,$time2])->select();
//        dump($this->getLastSql());

        //设置默认值
        $itemmodel = new Item();
        $chilrenitem = $itemmodel->where('pid',2)->select();//所有的血糖子项目
        $checkarr = array();
        foreach ($chilrenitem as $ci => $cv){//每一个都为空
            $checkarr[$cv['itemname']] = array(
                'cid' => null,
                'itemid' => $cv['itemid'],
                'date' => null,
                'value' => null,
                'note' => null,
                'itemname' => $cv['itemname'],
                'pid' => 2
            );
        }


        if(!empty($Beforeeatinglist)){
            foreach ($Beforeeatinglist as $i => $v){
                $Beforeeatinglist[$i]['time'] = date('H:i',strtotime($Beforeeatinglist[$i]['date']));
                $itemidsarr = explode(',',$v['itemid'] );
                $itemid = $itemidsarr[1];
                $item = $itemmodel->where('itemid',$itemid)->find();
                $Beforeeatinglist[$i]['itemname'] = $item['itemname'];



                //把结果写入结果数据，对号入座
                $checkarr[$item['itemname']]['cid'] =  $v['cid'];
                $checkarr[$item['itemname']]['itemid'] =  $itemid;
                $checkarr[$item['itemname']]['pid'] =  $itemidsarr[0];
                $checkarr[$item['itemname']]['date'] =  $v['date'];
                $checkarr[$item['itemname']]['value'] =  $v['value'];
                $checkarr[$item['itemname']]['note'] =  $v['note'];



            }

            return $checkarr;
        }else{
            return $checkarr;
        }


    }




    /*
     * 查看当天的血糖数据是否完整
     *
     *
     *
     * */
    public function isBsCompletePiecewise($bsl){
        if (!is_array($bsl)){
            return false;
        }

        foreach ($bsl as $v){
            if ( empty($v['date'])){//有一个没填都不行
                return false;
            }
        }


        return true;

    }



    /*
     * 某天胰岛素分段量
     * @param array $btttmsg  起始治疗方案数据
     * @param array $bfel  餐前大剂量列表
     * @param array $bsl  血糖列表
     * @param array $bsllast  前一天的血糖列表
     *
     *
     * */
    public function getInsulinPiecewise($btttmsg,$bfel,$bsl,$bsllast){

//        $iparr = array(0,0,0,0,0,0,0,0);//胰岛素分段量数组，数组8端值
        $iparr = array(
            '夜间3点'=>0,
            '早餐前'=>0,
            '早餐后'=>0,
            '午餐前'=>0,
            '午餐后'=>0,
            '晚餐前'=>0,
            '晚餐后'=>0,
            '睡前'=>0,
        );//胰岛素分段量数组，数组8端值
       /* foreach ($bfel as $bfeli => $bfelv){//化为字典数组
            $bfel[$bfelv['itemname']] = $bfelv;
        }
        foreach ($bsl as $bsli => $bslv){//化为字典数组
            $bsl[$bslv['itemname']] = $bslv;
        }*/



        //餐前大剂量

        /*if (!$this->isBsCompletePiecewise($bsl)){
            return $iparr = array(
                '夜间3点'=>'--',
                '早餐前'=>'--',
                '早餐后'=>'--',
                '午餐前'=>'--',
                '午餐后'=>'--',
                '晚餐前'=>'--',
                '晚餐后'=>'--',
                '睡前'=>'--',
            );//胰岛素分段量数组，数组8端值
        }*/


        //分段设计分钟
        $iptime[0] = changDateToMinutes5('0:00');//分段设计分钟整5数
        $iptime[1] = changDateToMinutes5('3:00');//分段设计分钟整5数
        $iptime[2] = changDateToMinutes5('7:00');//分段设计分钟整5数
        $iptime[3] = changDateToMinutes5('12:00');//分段设计分钟整5数
        $iptime[4] = changDateToMinutes5('17:00');//分段设计分钟整5数
        $iptime[5] = changDateToMinutes5('22:00');//分段设计分钟整5数
        $iptime[6] = 1440;//24:00分段设计分钟整5数


        //记录血糖时间
        $bstime[0] =  changDateToMinutes5($bsl['夜间3点']['date']);//夜间3点分钟整5数
        $bstime[1] =  changDateToMinutes5($bsl['早餐前']['date']);//早餐前分钟整5数
        $bstime[2] =  changDateToMinutes5($bsl['早餐后']['date']);//早餐后分钟整5数
        $bstime[3] =  changDateToMinutes5($bsl['午餐前']['date']);//午餐前分钟整5数
        $bstime[4] =  changDateToMinutes5($bsl['午餐后']['date']);//午餐后分钟整5数
        $bstime[5] =  changDateToMinutes5($bsl['晚餐前']['date']);//晚餐前分钟整5数
        $bstime[6] =  changDateToMinutes5($bsl['晚餐后']['date']);//晚餐后分钟整5数
        $bstime[7] =  changDateToMinutes5($bsl['睡前']['date']);//睡前分钟整5数
        $bstime[8] =  changDateToMinutes5($bsllast['睡前']['date']);//前一天睡前分钟整5数


//        dump($bsl);




        /*夜间3点*/
        //3点的时间判断
        if (empty($bsl['夜间3点']['date']) || empty($bsllast['睡前']['date']) ){
//            echo '11';
            $iparr['夜间3点'] = '--';
        }else{
            if($bstime[0]<$iptime[1]){
                //比分段时间小，计算0-$bstime[0]的血糖值
                $iparr['夜间3点'] += $btttmsg['sulin0003']/12* (  abs($bstime[0]-$iptime[0])/5  ) ;
            }else if($bstime[0]>=$iptime[1]){
//            echo ($btttmsg['sulin0003'])."/12*".(abs($bstime[0]-$iptime[0])/5);
                $iparr['夜间3点'] += $btttmsg['sulin0003']/12* (  abs($iptime[1]-$iptime[0])/5  );//0到3点的分段胰岛素
                $iparr['夜间3点'] += $btttmsg['sulin0307']/12* (  abs($bstime[0]-$iptime[1])/5  ) ;//3点到$bstime[0]的分段胰岛素
            }
            //0点的时间判断
            if($bstime[8]>$iptime[1]){
                //今天0点前睡觉
                //比0点时间小，计算$bstime[8]-$iptime[0]的血糖值
                $iparr['夜间3点'] += $btttmsg['sulin2224']/12* (  abs($iptime[6]-$bstime[8])/5  ) ;
            }else if($bstime[8]<$iptime[1]){
                ////0点之后睡觉
                //那要减掉那部分
                $iparr['夜间3点'] -= $btttmsg['sulin0003']/12* (  abs($bstime[8]-$iptime[0])/5  );//0到3点的分段胰岛素

            }
        }






        /*早餐前*/
        //7点的时间判断

        if (empty($bsl['夜间3点']['date']) || empty($bsl['早餐前']['date']) ){
//            echo '11';
            $iparr['早餐前'] = '--';
        }else{
            if($bstime[1]<$iptime[2]){
                //比分段时间小，计算3-$bstime[1]的血糖值
                $iparr['早餐前'] += $btttmsg['sulin0307']/12* (  abs($bstime[1]-$iptime[1])/5  ) ;
            }else{
//            echo ($btttmsg['sulin0003'])."/12*".(abs($bstime[0]-$iptime[0])/5);
                $iparr['早餐前'] += $btttmsg['sulin0307']/12* (  abs($iptime[2]-$iptime[1])/5  );//3到7点的分段胰岛素
                $iparr['早餐前'] += $btttmsg['sulin0712']/12* (  abs($bstime[1]-$iptime[2])/5  ) ;//7点到$bstime[0]的分段胰岛素
            }
            //3点的时间判断
            if($bstime[0]<$iptime[1]){
                //比3点时间小，计算$iptime[1]-$bstime[0]的血糖值
                $iparr['早餐前'] += $btttmsg['sulin0003']/12* (  abs($iptime[1]-$bstime[0])/5  ) ;
            }else {
                //那要减掉那部分

                $iparr['早餐前'] -= $btttmsg['sulin0307']/12* (  abs($bstime[0]-$iptime[1])/5  );//3到7点的分段胰岛素

            }

        }




        /*早餐后胰岛素量*/


        if (empty($bsl['早餐后']['date']) || empty($bsl['早餐前']['date']) ){
//            echo '11';
            $iparr['早餐后'] = '--';
        }else{
            //9点的时间判断 ,与12点判断

            if($bstime[2]<$iptime[3]){
                //比12点小，一般情况
                $iparr['早餐后'] += $btttmsg['sulin0712']/12* (  abs($bstime[2]-$iptime[2])/5  ) ;

            }else{
                //比12点大的话
                $iparr['早餐后'] += $btttmsg['sulin0712']/12* (  abs($iptime[3]-$iptime[2])/5  ) ;
                $iparr['早餐后'] += $btttmsg['sulin1217']/12* (  abs($bstime[2]-$iptime[3])/5  ) ;

            }






            //7点的时间判断
            if($bstime[1]<$iptime[2]){
                //比7点时间小，计算$iptime[2]-$bstime[1]的血糖值
                $iparr['早餐后'] += $btttmsg['sulin0307']/12* (  abs($iptime[2]-$bstime[1])/5  ) ;
            }else {
                //那要减掉那部分
                $iparr['早餐后'] -= $btttmsg['sulin0712']/12* (  abs($bstime[1]-$iptime[2])/5  );//3到7点的分段胰岛素
            }

            $iparr['早餐后']+=$bfel['早餐']['value'];//早餐大剂量】早餐大剂量加在早餐后胰岛素量

        }


















        /*午餐前胰岛素量*/

        if (empty($bsl['早餐后']['date']) || empty($bsl['午餐前']['date']) ){
//            echo '11';
            $iparr['午餐前'] = '--';
        }else{
            //12点的时间判断，
            if($bstime[3]<$iptime[3]){
                //比分段时间小，计算$bstime[3]-$bstime[2]的血糖值
                $iparr['午餐前'] += $btttmsg['sulin0712']/12* (  abs($bstime[3]-$bstime[2])/5  ) ;
            }else{

//                $iparr['午餐前'] += $btttmsg['sulin0712']/12* (  abs($iptime[3]-$bstime[2])/5  );//$bstime[2]到12点的分段胰岛素
//                $iparr['午餐前'] += $btttmsg['sulin1217']/12* (  abs($bstime[3]-$iptime[3])/5  ) ;//7点到$bstime[0]的分段胰岛素


                //早餐前与12点判断
                if($bstime[2]<$iptime[3]){
                    //比12点小，一般情况
                    $iparr['午餐前'] += $btttmsg['sulin0712']/12* (  abs($iptime[3]-$bstime[2])/5  );//$bstime[2]到12点的分段胰岛素
                    $iparr['午餐前'] += $btttmsg['sulin1217']/12* (  abs($bstime[3]-$iptime[3])/5  ) ;//7点到$bstime[0]的分段胰岛素

                }else{
                    //比12点大的话
                    $iparr['午餐前'] += $btttmsg['sulin1217']/12* (  abs($bstime[2]-$iptime[3])/5  );//$bstime[2]到12点的分段胰岛素
                    $iparr['午餐前'] += $btttmsg['sulin1217']/12* (  abs($bstime[3]-$bstime[2])/5  );//$bstime[2]到12点的分段胰岛素

                }
          
            }

           
            

        }













        /*午餐后（2h）14:15*/
        if (empty($bsl['午餐后']['date']) || empty($bsl['午餐前']['date']) ){
//            echo '11';
            $iparr['午餐后'] = '--';
        }else{
            //17点的时间判断，没有交集，直接判断
            $iparr['午餐后'] += $btttmsg['sulin1217']/12* (  abs($bstime[4]-$iptime[3])/5  ) ;
            //12点的时间判断
            if($bstime[3]<$iptime[3]){
                //比12点时间小，计算$iptime[3]-$bstime[3]的血糖值
                $iparr['午餐后'] += $btttmsg['sulin0712']/12* (  abs($iptime[3]-$bstime[3])/5  ) ;
            }else {
                //那要减掉那部分
                $iparr['午餐后'] -= $btttmsg['sulin1217']/12* (  abs($bstime[3]-$iptime[3])/5  );//3到7点的分段胰岛素
            }
            $iparr['午餐后']+=$bfel['午餐']['value'];//午餐大剂量加在午餐后胰岛素量



        }



        /*晚餐前（空腹）18:45*/
        if (empty($bsl['午餐后']['date']) || empty($bsl['晚餐前']['date']) ){
//            echo '11';
            $iparr['晚餐前'] = '--';
        }else{

            //17点的时间判断，
            if($bstime[5]<$iptime[4]){
                //比分段时间小，计算$bstime[3]-$bstime[2]的血糖值
                $iparr['晚餐前'] += $btttmsg['sulin1217']/12* (  abs($bstime[5]-$bstime[4])/5  ) ;
            }else{

                $iparr['晚餐前'] += $btttmsg['sulin1217']/12* (  abs($iptime[4]-$bstime[4])/5  );//$bstime[4]到17点的分段胰岛素
                $iparr['晚餐前'] += $btttmsg['sulin1722']/12* (  abs($bstime[5]-$iptime[4])/5  ) ;//7点到$bstime[0]的分段胰岛素
            }

        }











        /*晚餐后（2h）21:00*/
        //22点的时间判断


        if (empty($bsl['晚餐后']['date']) || empty($bsl['晚餐前']['date']) ){
//            echo '11';
            $iparr['晚餐后'] = '--';
        }else{

            if($bstime[6]<$iptime[5]){
                //比分段时间小，计算3-$bstime[1]的血糖值
                $iparr['晚餐后'] += $btttmsg['sulin1722']/12* (  abs($bstime[6]-$iptime[4])/5  ) ;
            }else{
                $iparr['晚餐后'] += $btttmsg['sulin1722']/12* (  abs($iptime[5]-$iptime[4])/5  );//17到22点的分段胰岛素
                $iparr['晚餐后'] += $btttmsg['sulin2224']/12* (  abs($bstime[6]-$iptime[5])/5  ) ;//22点到$bstime[0]的分段胰岛素
            }
            //17点的时间判断
            if($bstime[5]<$iptime[4]){
                //比17点时间小，计算$iptime[4]-$bstime[5]的血糖值
                $iparr['晚餐后'] += $btttmsg['sulin1217']/12* (  abs($iptime[4]-$bstime[5])/5  ) ;
            }else {
                //那要减掉那部分
                $iparr['晚餐后'] -= $btttmsg['sulin1722']/12* (  abs($bstime[5]-$iptime[4])/5  ); //17点到$bstime[5]

            }

            $iparr['晚餐后']+=$bfel['晚餐']['value'];//晚餐大剂量加在晚餐后胰岛素量

        }






        /*睡前23:20*/
        //24点的时间判断

        if (empty($bsl['晚餐后']['date']) || empty($bsl['睡前']['date']) ){
//            echo '11';
            $iparr['睡前'] = '--';
        }else{
            if($bstime[7]>$iptime[1]){
                //0点前睡觉，计算22-$bstime[7]的血糖值
                $iparr['睡前'] += $btttmsg['sulin2224']/12* (  abs($bstime[7]-$iptime[5])/5  ) ;
            }else{
                $iparr['睡前'] += $btttmsg['sulin2224']/12* (  abs($iptime[6]-$iptime[5])/5  );//22到24点的分段胰岛素
                $iparr['睡前'] += $btttmsg['sulin0003']/12* (  abs($bstime[7]-$iptime[0])/5  ) ;//0点到$bstime[7]的分段胰岛素
            }

            //22点的时间判断
            if($bstime[6]<$iptime[5]){
                $iparr['睡前'] += $btttmsg['sulin1722']/12* (  abs($iptime[5]-$bstime[6])/5  ) ;
            }else {
                //那要减掉那部分
                $iparr['睡前'] -= $btttmsg['sulin2224']/12* (  abs($bstime[6]-$iptime[5])/5  ); //17点到$bstime[5]
            }
        }

        




        $iparr = array_map('round2',$iparr );

       


        return $iparr;










        /*array(21) {
  ["brid"] => int(4)
  ["uid"] => int(4)
  ["hospital"] => string(12) "人民医院"
  ["insulin"] => string(1) "2"
  ["insulinamountday"] => string(5) "15.80"
  ["baseinsulin"] => string(4) "9.90"
  ["beforeeatinsulin"] => string(5) "17.60"
  ["sulin0003"] => string(4) "0.30"
  ["sulin0307"] => string(4) "0.60"
  ["sulin0712"] => string(4) "0.40"
  ["sulin1217"] => string(4) "0.30"
  ["sulin1722"] => string(4) "0.50"
  ["sulin2224"] => string(4) "0.30"
  ["breakfastsulin"] => string(4) "5.80"
  ["lunchsulin"] => string(4) "5.90"
  ["dinnerinsulin"] => string(4) "5.90"
  ["isotherdisease"] => string(1) "1"
  ["otherdisease"] => string(9) "心脏病"
  ["delete_time"] => NULL
  ["create_time"] => string(19) "2018-12-05 08:44:24"
  ["update_time"] => string(19) "2018-12-05 22:27:42"
}
array(3) {
  [0] => array(7) {
    ["cid"] => int(19)
    ["itemid"] => string(4) "2,12"
    ["date"] => string(19) "2018-12-05 08:00:34"
    ["value"] => string(4) "5.80"
    ["note"] => NULL
    ["time"] => string(5) "08:00"
    ["itemname"] => string(6) "早餐"
  }
  [1] => array(7) {
    ["cid"] => int(20)
    ["itemid"] => string(4) "2,13"
    ["date"] => string(19) "2018-12-05 14:30:34"
    ["value"] => string(4) "5.90"
    ["note"] => NULL
    ["time"] => string(5) "14:30"
    ["itemname"] => string(6) "午餐"
  }
  [2] => array(7) {
    ["cid"] => int(30)
    ["itemid"] => string(4) "2,14"
    ["date"] => string(19) "2018-12-05 21:20:00"
    ["value"] => string(4) "5.90"
    ["note"] => NULL
    ["time"] => string(5) "21:20"
    ["itemname"] => string(6) "晚餐"
  }
}
array(8) {
  [0] => array(9) {
    ["cid"] => int(22)
    ["itemid"] => string(3) "1,5"
    ["date"] => string(19) "2018-12-05 03:10:34"
    ["value"] => string(5) "11.00"
    ["health"] => string(1) "1"
    ["type"] => string(1) "3"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(10) "夜间3点"
  }
  [1] => array(9) {
    ["cid"] => int(23)
    ["itemid"] => string(3) "1,6"
    ["date"] => string(19) "2018-12-05 07:30:26"
    ["value"] => string(4) "4.00"
    ["health"] => string(1) "2"
    ["type"] => string(1) "1"
    ["note"] => NULL
    ["reason"] => string(0) ""
    ["itemname"] => string(9) "早餐前"
  }
  [2] => array(9) {
    ["cid"] => int(24)
    ["itemid"] => string(3) "1,7"
    ["date"] => string(19) "2018-12-05 09:30:34"
    ["value"] => string(5) "30.00"
    ["health"] => string(1) "5"
    ["type"] => string(1) "2"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(9) "早餐后"
  }
  [3] => array(9) {
    ["cid"] => int(25)
    ["itemid"] => string(3) "1,8"
    ["date"] => string(19) "2018-12-05 12:00:00"
    ["value"] => string(5) "15.00"
    ["health"] => string(1) "4"
    ["type"] => string(1) "2"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(9) "午餐前"
  }
  [4] => array(9) {
    ["cid"] => int(26)
    ["itemid"] => string(3) "1,9"
    ["date"] => string(19) "2018-12-05 14:15:00"
    ["value"] => string(5) "17.00"
    ["health"] => string(1) "5"
    ["type"] => string(1) "2"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(9) "午餐后"
  }
  [5] => array(9) {
    ["cid"] => int(27)
    ["itemid"] => string(4) "1,10"
    ["date"] => string(19) "2018-12-05 18:45:00"
    ["value"] => string(4) "3.80"
    ["health"] => string(1) "3"
    ["type"] => string(1) "1"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(9) "晚餐前"
  }
  [6] => array(9) {
    ["cid"] => int(28)
    ["itemid"] => string(4) "1,11"
    ["date"] => string(19) "2018-12-05 21:00:00"
    ["value"] => string(5) "12.00"
    ["health"] => string(1) "4"
    ["type"] => string(1) "2"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(9) "晚餐后"
  }
  [7] => array(9) {
    ["cid"] => int(29)
    ["itemid"] => string(3) "1,4"
    ["date"] => string(19) "2018-12-05 23:20:00"
    ["value"] => string(4) "7.00"
    ["health"] => string(1) "1"
    ["type"] => string(1) "3"
    ["note"] => NULL
    ["reason"] => NULL
    ["itemname"] => string(6) "睡前"
  }
}*/
        
        
    }




        
    
}